<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/16
 * Time: 16:51
 */

namespace app\admin\controller;

use think\Controller;
use think\facade\Config;
use think\facade\Request;
use think\facade\Session;

class Base extends Controller
{
    public function initialize()
    {
        $this->isLogin();
    }

    # 判断是否重复登陆
    protected function logined(){
        if (Session::has('admin_info')){
            $this->redirect('/index');
        }
    }

    # 判断是否登陆
    protected function isLogin(){
        if ( ! Session::has('admin_info')){
            if (Request::isAjax()){
                $this->ckJsonReturn('您还未登陆，请先登陆');
            }else{
                $this->error('您还未登陆，请先登陆', '/login');
            }
        }
    }

    # json返回
    protected function ckJsonReturn($msg = '', $status = 0, $data = []){
        $msg = $msg ?: Config::get('sys_err_msg');
        echo json_encode([
            'status'=>$status,
            'msg'=>$msg,
            'data'=>$data
        ]);
        exit;
    }

    # 判断是否是ajax请求
    protected function checkIsAjax($msg = ''){
        if ( ! Request::isAjax()){
            $msg = $msg ?: Config::get('sys_err_msg');
            $this->error($msg);
        }
    }
}